package com.circle.rest.modular.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.circle.rest.core.aop.Log;
import com.circle.rest.core.aop.Permission;
import com.circle.rest.core.common.enums.BusinessType;
import com.circle.rest.core.common.model.SuccessResponseData;
import com.circle.rest.core.exception.ServiceException;
import com.circle.rest.core.util.ToolUtil;
import com.circle.rest.modular.sys.model.Dept;
import com.circle.rest.modular.sys.service.IDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 部门信息 前端控制器
 * </p>
 *
 * @author 赵航
 * @since 2019-04-03
 */
@Api(value = "部门信息", tags = {"sys : 部门信息API"})
@RestController
@RequestMapping("/sys/dept")
public class DeptController {

    @Autowired
    private IDeptService deptService;

    /**
     * 查询部门信息列表
     */
    @ApiOperation("查询部门信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "condition", value = "关键字", dataType = "String")
    })
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @Log(title = "查询部门信息列表", businessType = BusinessType.SELECT_LIST)
    @Permission
    public Object list(String condition) {
        QueryWrapper<Dept> wrapper = new QueryWrapper<>();
        if (ToolUtil.isNotEmpty(condition)) {
            wrapper.like("full_name", condition).or().like("short_name", condition);
        }
        List<Map<String, Object>> maps = deptService.listMaps(wrapper);
        return new SuccessResponseData(maps);
    }

    /**
     * 添加部门信息
     */
    @ApiOperation("添加部门信息")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @Log(title = "添加部门信息", businessType = BusinessType.INSERT)
    @Permission
    public Object add(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        dept.setCreateTime(LocalDateTime.now());
        boolean save = deptService.save(dept);
        return new SuccessResponseData(save);
    }

    /**
     * 修改部门信息
     */
    @ApiOperation("修改部门信息")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @Log(title = "修改部门信息", businessType = BusinessType.UPDATE)
    @Permission
    public Object edit(Dept dept) {
        if (ToolUtil.isEmpty(dept.getId())) {
            throw new ServiceException("编号不能为空");
        }
        if (ToolUtil.isEmpty(dept.getVersion())) {
            throw new ServiceException("版本号不能为空");
        }
        dept.setUpdateTime(LocalDateTime.now());
        if (!deptService.updateById(dept)) {
            throw new ServiceException("更新失败");
        }
        return new SuccessResponseData();
    }

    /**
     * 删除部门信息
     */
    @ApiOperation("删除部门信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "部门信息编号", required = true, dataType = "String")
    })
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    @Log(title = "删除部门信息", businessType = BusinessType.DELETE)
    @Permission
    public Object delete(String id) {
        if (ToolUtil.isEmpty(id)) {
            throw new ServiceException("部门信息编号不能为空");
        }
        Dept deptOld = deptService.getById(id);
        if (ToolUtil.isEmpty(deptOld)) {
            throw new ServiceException("部门信息不存在，删除失败");
        }
        if (!deptService.removeById(id)) {
            throw new ServiceException("删除失败");
        }
        return new SuccessResponseData();
    }

    /**
     * 查询部门信息
     */
    @ApiOperation("查询部门信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "部门信息编号", required = true, dataType = "String")
    })
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @Log(title = "查询部门信息", businessType = BusinessType.SELECT_ONE)
    @Permission
    public Object detail(String id) {
        if (ToolUtil.isEmpty(id)) {
            throw new ServiceException("部门信息编号不能为空");
        }
        Dept dept = deptService.getById(id);
        if (ToolUtil.isEmpty(dept)) {
            throw new ServiceException("部门信息不存在");
        }
        return new SuccessResponseData(dept);
    }
}
